{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Memory in the Multi-Input Chain\n",
    "Most memory objects assume a single input. In this notebook, we go over how to add memory to a chain that has multiple inputs. We will add memory to a question/answering chain. This chain takes as inputs both related documents and a user question.\n",
    "\n",
    "大多数内存对象都假定只有一个输入。在本笔记本中，我们将介绍如何向具有多个输入的链添加内存。我们将向问答链添加内存。该链将相关文档和用户问题作为输入。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "memory=ConversationBufferMemory(input_key='human_input', memory_key='chat_history') llm_chain=LLMChain(prompt=PromptTemplate(input_variables=['chat_history', 'context', 'human_input'], template='You are a chatbot having a conversation with a human.\\n\\nGiven the following extracted parts of a long document and a question, create a final answer.\\n\\n{context}\\n\\n{chat_history}\\nHuman: {human_input}\\nChatbot:'), llm=OpenAI(client=<openai.resources.completions.Completions object at 0x0000020C24047B90>, async_client=<openai.resources.completions.AsyncCompletions object at 0x0000020C23C1DD90>, temperature=0.0, openai_api_key=SecretStr('**********'), openai_proxy='')) document_variable_name='context'\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'output_text': ' The current version of langchain_chroma is 0.1.2. '}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_chroma import Chroma\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "from langchain_text_splitters import CharacterTextSplitter\n",
    "from langchain.chains.question_answering import load_qa_chain\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "from langchain_core.prompts import PromptTemplate\n",
    "from langchain_openai import OpenAI\n",
    "\n",
    "with open(\"../requirements_0_1.txt\") as f:\n",
    "    state_of_the_union = f.read()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "texts = text_splitter.split_text(state_of_the_union)\n",
    "\n",
    "embeddings = OpenAIEmbeddings()\n",
    "docsearch = Chroma.from_texts(\n",
    "    texts, embeddings, metadatas=[{\"source\": i} for i in range(len(texts))]\n",
    ")\n",
    "query = \"What is langchain_chroma version?\"\n",
    "docs = docsearch.similarity_search(query)\n",
    "# print(docs[0].page_content)\n",
    "\n",
    "template = \"\"\"You are a chatbot having a conversation with a human.\n",
    "\n",
    "Given the following extracted parts of a long document and a question, create a final answer.\n",
    "\n",
    "{context}\n",
    "\n",
    "{chat_history}\n",
    "Human: {human_input}\n",
    "Chatbot:\"\"\"\n",
    "\n",
    "prompt = PromptTemplate(\n",
    "    input_variables=[\"chat_history\", \"human_input\", \"context\"], template=template\n",
    ")\n",
    "memory = ConversationBufferMemory(memory_key=\"chat_history\", input_key=\"human_input\")\n",
    "chain = load_qa_chain(\n",
    "    OpenAI(temperature=0), chain_type=\"stuff\", memory=memory, prompt=prompt\n",
    ")\n",
    "print(chain)\n",
    "query = \"What is langchain_chroma version?\"\n",
    "chain({\"input_documents\": docs, \"human_input\": query}, return_only_outputs=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Human: What is langchain_chroma version?\n",
      "AI:  The langchain_chroma version is 0.1.2.\n"
     ]
    }
   ],
   "source": [
    "print(chain.memory.buffer)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "langchain0_1",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
